package com.sikaryofficial.backend.manager.information;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sikaryofficial.backend.annotation.TenantFilter;
import com.sikaryofficial.backend.constant.ArticleAuditStatusEnum;
import com.sikaryofficial.backend.constant.TagStatus;
import com.sikaryofficial.backend.domain.dto.req.information.ArticleTagReq;
import com.sikaryofficial.backend.domain.dto.resp.information.ArticleTagDTO;
import com.sikaryofficial.backend.domain.entity.Article;
import com.sikaryofficial.backend.domain.entity.ArticleTag;
import com.sikaryofficial.backend.domain.mapping.ArticleTagMapping;
import com.sikaryofficial.backend.service.IArticleService;
import com.sikaryofficial.backend.service.IArticleTagService;
import com.sikaryofficial.common.core.domain.R;
import com.sikaryofficial.common.core.exception.ServiceException;
import com.sikaryofficial.common.security.utils.SecurityUtils;
import com.sikaryofficial.system.api.RemoteArticleService;
import com.sikaryofficial.system.api.model.infomation.ArticleDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * @author : qinjinyuan
 * @desc : 文章banner聚合类
 * @date : 2024/04/10 10:46
 */
@Component
@Slf4j
@SuppressWarnings(value = "unchecked")
public class ArticleTagManager {

    @Autowired
    private IArticleTagService articleTagService;
    @Autowired
    private IArticleService articleService;
    @Autowired
    private RemoteArticleService remoteArticleService;

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void saveOrUpdate(ArticleTagReq articleTag) {
        // 判定文章是否审核通过
        Article article = articleService.getOne(new LambdaQueryWrapper<Article>()
                .select(Article::getArticleId, Article::getWeight, Article::getHasBlocked, Article::getCreatedBy)
                .eq(Article::getArticleId, articleTag.getArticleId())
                .eq(Article::getAuditStatus, ArticleAuditStatusEnum.APPROVED.getCode())
                .eq(Article::getDeletedVersion, 0L)
        );
        if (Objects.isNull(article) || article.getHasBlocked() > 0) {
            throw new ServiceException("Audit Status need to be approved or blocked");
        }
        log.info("saveOrUpdate articleTag:{}", articleTag);
        // 更新标签状态:启用-> 禁用
        articleTagService.lambdaUpdate().ne(ArticleTag::getArticleId, articleTag.getArticleId())
                .eq(ArticleTag::getTagEnable, TagStatus.ENABLED.getCode())
                .set(ArticleTag::getTagEnable, TagStatus.DISABLED.getCode())
                .set(ArticleTag::getUpdatedBy, SecurityUtils.getUserId())
                .set(ArticleTag::getUpdatedTime, new Date())
                .update();
        // 更新当前指定tag数据及状态
        articleTagService.saveTag(articleTag);
    }

    public ArticleTagDTO currentTag() {
        ArticleTag articleTag = articleTagService.lambdaQuery().eq(ArticleTag::getDeletedVersion, 0L)
                .eq(ArticleTag::getTagEnable, TagStatus.ENABLED.getCode())
                .last(" limit 1 ")
                .one();

        ArticleTagDTO articleTagDTO = ArticleTagMapping.INSTANCE.coverToDTO(articleTag);
        if (Objects.isNull(articleTagDTO)) {
            return new ArticleTagDTO();
        }
        // 查询文章标题
        R<ArticleDTO> result = remoteArticleService.getInfo(articleTagDTO.getArticleId());
        if (Objects.isNull(result) || Objects.isNull(result.getData())) {
            throw new ServiceException("Article is not exist.");
        }
        articleTagDTO.setHeadline(result.getData().getHeadline());
        return articleTagDTO;
    }
}
